package com.andcard.action;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.andcard.AndCard;
import com.andcard.R;
import com.andcard.obj.Card;
import com.andcard.sql.DbAdapter;
import com.andcard.util.Reference;

public class TabCard extends AbsAction implements OnClickListener{

	// some references
	
	// buttons
	private Button btOk;
	private Button btKo;
	private Button btStart;
	private Button btMore;
	private Button btAnswer;
	// hidden buttons
	private Button btDelete;
	private Button btStats;
	private Button btBack;
	
	// array contains row buttons
	private Button[] firstRowButtons;
	private Button[] secondRowButtons;
	
	// textview
	private TextView textViewQuestion;
	private TextView textViewAnswer;
	
	private int counter = -1;
	
	public TabCard(AndCard activity, DbAdapter dbAdapter){
		
		super(activity, dbAdapter);
		
		Log.d(Reference.TAG, "TabCard.Constructor");
		
		// buttons
		btOk = (Button)activity.findViewById(R.id.btOk);
		btKo = (Button)activity.findViewById(R.id.btKo);
		btStart = (Button)activity.findViewById(R.id.btStart);
		btMore = (Button)activity.findViewById(R.id.btMore);
		btAnswer = (Button)activity.findViewById(R.id.btAnswer);
		btDelete = (Button)activity.findViewById(R.id.btDelete);
		btStats = (Button)activity.findViewById(R.id.btStats);
		btBack = (Button)activity.findViewById(R.id.btBack);
		
		btOk.setOnClickListener(this);
		btKo.setOnClickListener(this);
		btStats.setOnClickListener(this);
		btMore.setOnClickListener(this);
		btAnswer.setOnClickListener(this);
		btBack.setOnClickListener(this);
		btDelete.setOnClickListener(this);
		btStart.setOnClickListener(this);
		
		textViewQuestion = (TextView)activity.findViewById(R.id.textViewQuestion);
		textViewAnswer = (TextView)activity.findViewById(R.id.textViewAnswer);
		
		// populate array
		firstRowButtons = new Button[]{btOk, btKo, btStart, btMore};
		secondRowButtons = new Button[]{btDelete, btStats, btBack};
		
		// disable all buttons
		this.disableButtons();
	}
	
	public void onClick(View v) {
		
		Log.d(Reference.TAG, "TabCard.onClick - " + v.toString() );
		
        // Perform action on click
		if (v.equals(btMore)){
			Log.d(Reference.TAG, "TabCard.onClick - btMore" );
			changeVisibility(firstRowButtons, secondRowButtons);
		}else if(v.equals(btBack)){
			Log.d(Reference.TAG, "TabCard.onClick - btBack" );
			changeVisibility(secondRowButtons, firstRowButtons);
		}else if (v.equals(btDelete)){
			Log.d(Reference.TAG, "TabCard.onClick - btDelete" );
			checkDeleteCard();
		}else if (v.equals(btStats)){
			Log.d(Reference.TAG, "TabCard.onClick - btStats" );
			stats();
		}else if (v.equals(btOk)){
			Log.d(Reference.TAG, "TabCard.onClick - btOk" );
			ok();
		}else if (v.equals(btKo)){
			Log.d(Reference.TAG, "TabCard.onClick - btKo" );
			ko();
		}else if (v.equals(btAnswer)){
			Log.d(Reference.TAG, "TabCard.onClick - btAnswer" );
			showResult();
		}else if (v.equals(btStart)){
			Log.d(Reference.TAG, "TabCard.onClick - btStart" );
			start();
		}
    }
	
	public void start(){
		Log.d(Reference.TAG, "TabCard.stats - start" );
		
		if (this.getDbAdapter().getListCard() == null || this.getDbAdapter().getListCard().size() == 0){
			this.getActivity().showDialog(Reference.ALERT_NO_CARDS);
			return;
		}
		
		this.next();
		this.enableAllNoStart();
	}
	
	public void stats(){
		
		Log.d(Reference.TAG, "TabCard.stats - stats" );
		
		Card card = this.getDbAdapter().getCard(counter);
		int totAnswer = card.getOkAnswer() + card.getKoAnswer();
		
		double percent = 0;
		if (totAnswer != 0){
			percent = card.getOkAnswer() / card.getKoAnswer() * 100;
		}
		
		Log.d(Reference.TAG, "TabCard.stats - percent " + percent );
	
		String strStats = "Current Iteration: " + card.getIteration() + "\nOk:" + card.getOkAnswer() + " - Ko: " + card.getKoAnswer() + "\n%OK: " + percent;
		// for debug mode
		strStats += "\nId: " + card.getId();
		this.textViewAnswer.setText(strStats);
		
	}
	
	public void ok(){
		
		Log.d(Reference.TAG, "TabCard.stats - ok" );
		
		if (this.getDbAdapter().isDatabaseReadOnly() == false && this.getDbAdapter().updateCardStatus(counter, true) == false){
			this.getActivity().showDialog(Reference.ALERT_NO_CARD_STATUS_UPDATED);
		}
		this.textViewAnswer.setText("");
		this.next();
	}
	
	public void ko(){
		
		Log.d(Reference.TAG, "TabCard.stats - ko" );
		
		if (this.getDbAdapter().isDatabaseReadOnly() == false && this.getDbAdapter().updateCardStatus(counter, false) == false){
			this.getActivity().showDialog(Reference.ALERT_NO_CARD_STATUS_UPDATED);
		}
		this.textViewAnswer.setText("");
		this.next();
	}
	
	public void showResult(){
		
		Log.d(Reference.TAG, "TabCard.stats - showResult" );
		
		if (counter != -1){
			this.textViewAnswer.setText(this.getDbAdapter().getCard(counter).getAnswer());
		}
	}
	
	 
	public void next(){
		
		Log.d(Reference.TAG, "TabCard.stats - next" );
		
		if (this.getDbAdapter().getListCard() == null || this.getDbAdapter().getListCard().size() == 0){
			this.getActivity().showDialog(Reference.ALERT_NO_CARDS);
			this.textViewAnswer.setText("");
			this.disableButtons();
			this.counter = -1;
			return;
		}
		
		counter++;
		
		if (counter == this.getDbAdapter().getListCard().size()){
			this.getActivity().showDialog(Reference.ALERT_END_OF_CARDS);
			this.textViewAnswer.setText("");
			this.textViewQuestion.setText("");
			this.disableButtons();
			return;
		}else{
			this.textViewQuestion.setText(this.getDbAdapter().getCard(counter).getQuestion());
		}

		return;
	}
	
	public void checkDeleteCard(){
		
		Log.d(Reference.TAG, "TabCard.stats - checkDeleteCard" );
		
		if (this.getDbAdapter().isDatabaseReadOnly()){
			this.getActivity().showDialog(Reference.ALERT_DATABASE_READ_ONLY);
			return;	
		}
		
		if (this.getDbAdapter().isDatabaseOpen() == true){
			this.getActivity().showDialog(Reference.ALERT_ACTION_DELETE_CARD);
		}else{
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_LOADED);
		}
	}
	
	public void deleteCard(){
		
		Log.d(Reference.TAG, "TabCard.stats - deleteCard" );
		
		if (this.getDbAdapter().deleteCard(counter)){
			this.getActivity().showDialog(Reference.ALERT_OK_DELETE);
			this.next();
		}else{
			this.getActivity().showDialog(Reference.ALERT_KO_DELETE);
		}
	}
	
	public void changeVisibility(Button[] oldRow, Button[] newRow){
		
		Log.d(Reference.TAG, "TabCard.stats - changeVisibility" );
		
		// disable oldRow
		for (int x = 0; x < oldRow.length; x++){
			oldRow[x].setVisibility(View.GONE);
		}
		
		// enable newRow
		for (int x = 0; x < newRow.length; x++){
			newRow[x].setVisibility(View.VISIBLE);
		}
	}
	
	public int getCounter(){
		return this.counter;
	}
	
	public void setCounter(int counter){
		this.counter = counter;
	}

	public void disableButtons(){
		btOk.setEnabled(false);
		btKo.setEnabled(false);
		btAnswer.setEnabled(false);
		btStats.setEnabled(false);
		btStart.setEnabled(false);
		btDelete.setEnabled(false);
	}
	
	public void disableAllNotStart(){
		btOk.setEnabled(false);
		btKo.setEnabled(false);
		btAnswer.setEnabled(false);
		btStats.setEnabled(false);
		btDelete.setEnabled(false);
		btStart.setEnabled(true);
		
		// clean textview
		textViewQuestion.setText("");
		textViewAnswer.setText("");
	}
	
	public void enableAllNoStart(){
		btOk.setEnabled(true);
		btKo.setEnabled(true);
		btAnswer.setEnabled(true);
		btStats.setEnabled(true);
		btDelete.setEnabled(true);
		btStart.setEnabled(false);
	}
}
